import { sys } from "cc";
import FrameBase from "../FrameBase";

export default class LocalStoreage extends FrameBase{
    private static _instance:LocalStoreage = null;
    public static get instance(){
        if(!this._instance){
            this._instance = new LocalStoreage();
        }
        return this._instance;
    }

    constructor(){
        super();
        if(sys.platform == sys.Platform.WECHAT_GAME){
            this.platform = globalThis.wx;
        }else if(sys.platform == sys.Platform.BYTEDANCE_MINI_GAME){
            this.platform = globalThis.tt;
        }
    }

    private platform = null;
    public setItem(key:string, value:any){
        if(sys.platform == sys.Platform.DESKTOP_BROWSER || sys.platform == sys.Platform.MOBILE_BROWSER){
            sys.localStorage.setItem(key, JSON.stringify(value));
        }else{
            this.platform.setStorageSync(key, value);
        }
    }

    public getItem(key:any):any{
        if(sys.platform == sys.Platform.DESKTOP_BROWSER || sys.platform == sys.Platform.MOBILE_BROWSER){
            return JSON.parse(sys.localStorage.getItem(key));
        }else{
            return this.platform.getStorageSync(key);
        }
    }

}